﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using CmdTailCmd;
using regx.Properties;
using regxExtensions;
using System.Linq;

namespace regx
{
    public static class DisplayHelp
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")] // Needs to be visible for call to HelpRequestCategory<>()
        public enum HelpCategories {Usage, RegexQuickRef, Matching, Replacing, LineHandling, Parameters, Format, Version, All}

        private class HelpInfo
        {   //=================================================================
            public string Title;
            public string Text;

            public HelpInfo(string sin)
            {   //-------------------------------------------------------------
                string HelpText = sin.Replace("%%%INSERT_VERSION_HERE%%%", Assembly.GetExecutingAssembly().GetName().Version.ToString());
                Title = string.Empty;
                Text  = HelpText;
                
                int l = HelpText.IndexOf('\r');
                if (-1 != l)
                {
                    string LineOne = HelpText.Substring(0, l);
                    
                    Regex rxTitle = new Regex(@"^Title:(?'title'.*)$");
                    Match m = rxTitle.Match(LineOne);
                    if (m.Success && m.Groups["title"].Success)
                    {   // We have a title line
                        Title = m.Groups["title"].Value.Trim();
                        Text  = HelpText.Substring(l + 1);
                        return;
                    }
                }
                
            }
        }

        public static bool Command(CmdTail tail)
        { //-------------------------------------------------------------------
            if (!tail.IsHelpRequest()) return false;

            var HelpText = new Dictionary<HelpCategories,HelpInfo>();
            HelpText.Add(HelpCategories.Usage,          new HelpInfo(Resources.HelpUsage));
            HelpText.Add(HelpCategories.RegexQuickRef,  new HelpInfo(Resources.HelpQuickRef));
            HelpText.Add(HelpCategories.Matching,       new HelpInfo(Resources.HelpMatching));
            HelpText.Add(HelpCategories.Replacing,      new HelpInfo(Resources.HelpReplacing));
            HelpText.Add(HelpCategories.LineHandling,   new HelpInfo(Resources.HelpLineHandling));
            HelpText.Add(HelpCategories.Parameters,     new HelpInfo(Resources.HelpParameters));
            HelpText.Add(HelpCategories.Format,         new HelpInfo(Resources.HelpFormat));
            HelpText.Add(HelpCategories.Version,        new HelpInfo(Resources.HelpVersion));

            HelpCategories cat = tail.HelpRequestCategory<HelpCategories>();
            if (HelpText.ContainsKey(cat))
            {
                Console.WriteLine(HelpText[cat].Text);
            }
            else
            {
                // Show everything
                foreach (HelpInfo hi in HelpText.Values)
                {
                    if (hi.Text.IsNotEmpty())
                    {
                        const int LineWidth = 78;
                        const char BoxChar = '=';
                        string Boxer = new string(BoxChar, LineWidth);
                        Console.WriteLine(Boxer);
                        
                        if (hi.Title.IsNotEmpty())
                        {
                            Regex rxCharacters = new Regex(@".");
                            const string Spacer = @"$& ";
                            string SpacedTitle = " " + rxCharacters.Replace(hi.Title, Spacer).Trim().ToUpper() + " ";
                            
                            if (SpacedTitle.Length < LineWidth)
                            {
                                int n = (LineWidth - SpacedTitle.Length)/2;
                                SpacedTitle = Boxer.OverwriteAt(n, SpacedTitle);
                            }
                            Console.WriteLine(SpacedTitle);
                            Console.WriteLine(Boxer);
                        }

                        Console.WriteLine(hi.Text);
                        Console.WriteLine();
                    }
                }
            }
            return true;
        }

    }
}
